package com.baijiayun.liveuibase.toolbox.responder;

import android.content.Context;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.res.ResourcesCompat;

import android.view.View;
import android.widget.FrameLayout;

import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.DisplayUtils;

import java.util.Random;

/**
 * Created by yongjiaming on 2019-05-24
 */
public class ResponderRobButton extends AppCompatImageView {

    private int widthAndHeight;

    public ResponderRobButton(Context context) {
        super(context);
    }

    public void setDefaultSize(int defaultSizeInDp){
        widthAndHeight = DisplayUtils.dip2px(getContext(), defaultSizeInDp);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthAndHeight, widthAndHeight);
    }

    public void changeRobButtonStatus(RobButtonState state) {
        switch (state) {
            case ROBED:
                setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.base_ic_responder_rob_win, null));
                break;
            case NORMAL:
                setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.base_ic_responder_rob_normal, null));
                break;
            case ROB_FAIL:
                setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.base_ic_responder_rob_lose, null));
                break;
            case READY_TO_SUBMIT:
                setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.base_ic_responder_rob_submit, null));
                break;
            default:
                break;
        }
    }

    /**
     * 随机改变坐标
     * @param parent
     */
    public void changePositionRandom(View parent){
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) getLayoutParams();
        Random random = new Random();
        layoutParams.leftMargin = random.nextInt(parent.getWidth() - widthAndHeight);
        layoutParams.topMargin = random.nextInt(parent.getHeight() - widthAndHeight);
        setLayoutParams(layoutParams);
    }


    public enum RobButtonState {
        NORMAL, ROBED, READY_TO_SUBMIT, ROB_FAIL
    }

}
